package com.loyalove.zerone.gateway;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

/**
 * @author loyalove
 */
@SpringBootConfiguration
public class GatewayConfiguration {

  @Bean
  public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("path_route", r -> r.path("/get")
            .uri("http://httpbin.org"))

        .route("host_route", r -> r.host("*.myhost.org")
            .uri("http://httpbin.org"))

        .route("rewrite_route", r -> r.host("*.rewrite.org")
            .filters(f -> f.rewritePath("/foo/(?<segment>.*)",
                "/${segment}"))
            .uri("http://httpbin.org"))

        .route("hystrix_route", r -> r.host("*.hystrix.org")
            .filters(f -> f.hystrix(c -> c.setName("slowcmd")))
            .uri("http://httpbin.org"))

        .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
            .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
            .uri("http://httpbin.org"))

        .route("limit_route", r -> r.host("*.limited.org")
            .and()
            .path("/anything/**")
            .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
            .uri("http://localhost:8761"))

        .route("websocket_route", r -> r.path("/echo")
            .uri("ws://localhost:9000"))

        .build();
  }

  @Bean
  RedisRateLimiter redisRateLimiter() {
    return new RedisRateLimiter(1, 2);
  }

  /*@Bean
  SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
    return http.httpBasic().and()
        .csrf().disable()
        .authorizeExchange()
        .pathMatchers("/anything/**").authenticated()
        .anyExchange().permitAll()
        .and()
        .build();
  }*/

 /* @Bean
  public MapReactiveUserDetailsService reactiveUserDetailsService() {
    UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build();
    return new MapReactiveUserDetailsService(user);
  }*/

}
